package cc.geguo.cookiedemo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者  Administrator
 * 时间  2017/2/16 19:17
 * 文件  SessionDemo
 * 描述
 */
public class Demo1 extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 创建Cookie对象
//		Cookie  cookie = new Cookie("name","geguo");
		Cookie  cookie2 = new Cookie("cook2","valiablePath");
		cookie2.setMaxAge(0);//删除同名cookie


//		cookie.setPath("ceshi/wuxiao");//当访问路径和设置路径相同时，才会回传到浏览器

		//2. 把cookie返回给浏览器（响应头）
//		resp.addCookie(cookie);
		resp.addCookie(cookie2);
		req.getSession().setAttribute("demo1","SessionValue");

//		3. 从请求头 获取cookie信息
		Cookie[] cookies = req.getCookies();
		if (cookies!=null){
			for (Cookie c : cookies) {
				System.out.println("name:"+c.getName());
			}
		}else {
			System.out.println("未获取到Cookie对象");
		}
	}
}
